package com.loit.loitcommon.json;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Lifh
 * @date 2022/12/20
 * 返回实体类
 */
public class AjaxJson<T> implements Serializable {
    public static final String CODE_COMMON_FAIL;
    private static final long serialVersionUID = -7790919285662399570L;
    private boolean success = true;
    private String code;
    private String msg;
    private Map<String, T> body;
    private T data;

    public static AjaxJson returnExceptionInfo(String info) {
        AjaxJson json = new AjaxJson();
        json.setSuccess(false);
        json.setCode(CODE_COMMON_FAIL);
        json.setMsg(info);
        return json;
    }

    public static AjaxJson returnExceptionInfo(String code, String info) {
        AjaxJson json = new AjaxJson();
        json.setSuccess(false);
        json.setCode(code);
        json.setMsg(info);
        return json;
    }

    public static AjaxJson returnExceptionInfo(String info, AjaxJson json) {
        json.setSuccess(false);
        json.setCode(CODE_COMMON_FAIL);
        json.setMsg(info);
        return json;
    }

    public static AjaxJson returnExceptionInfo(LoitStatusCode info) {
        AjaxJson json = new AjaxJson();
        json.setSuccess(false);
        json.setCode(info.statusCode);
        json.setMsg(info.statusDesc);
        return json;
    }

    public static AjaxJson returnExceptionInfo(LoitStatusCode info, AjaxJson json) {
        json.setSuccess(false);
        json.setCode(info.statusCode);
        json.setMsg(info.statusDesc);
        return json;
    }

    public AjaxJson(LoitStatusCode loitStatusCode, Boolean success, String msg) {
        this.code = LoitStatusCode.SUCESS.statusCode;
        this.msg = "操作成功";
        this.body = new LinkedHashMap();
        this.code = loitStatusCode.statusCode;
        this.success = success;
        this.msg = msg;
    }

    public AjaxJson() {
        this.code = LoitStatusCode.SUCESS.statusCode;
        this.msg = "操作成功";
        this.body = new LinkedHashMap();
    }

    public Map<String, T> getBody() {
        return this.body;
    }

    public void setBody(Map<String, T> body) {
        this.body = body;
    }

    public void put(String key, T value) {
        this.body.put(key, value);
    }

    public void remove(String key) {
        this.body.remove(key);
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String toString() {
        return "AjaxJson [success=" + this.success + ", code=" + this.code + ", msg=" + this.msg + "]";
    }

    static {
        CODE_COMMON_FAIL = LoitStatusCode.FAIL.statusCode;
    }
}
